package com.cvte.boe.media.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.LruCache;

import java.lang.ref.SoftReference;

/**
 * Create by liyang 2015/02/06
 *
 * @功能 通过resID加载资源并缓存到LruCache
 */
public class ResCacheLoadUtil {

    private static final int CAPACITY = 30;
    private static LruCache<Integer, SoftReference<Bitmap>> msBitmap = new LruCache(CAPACITY);
    private static LruCache<Integer, SoftReference<Drawable>> msDrawable = new LruCache(CAPACITY);
    private static LruCache<Integer, String> msString = new LruCache(CAPACITY);
    private static LruCache<String, SoftReference<Bitmap>> msImgCache = new LruCache(CAPACITY);

    public static synchronized void putCacheByImgPath(String imgPath, Bitmap bmp) {
        if ((bmp != null) && (!bmp.isRecycled())) {
            msImgCache.put(imgPath, new SoftReference<Bitmap>(bmp));
        }
    }

    public static synchronized Bitmap getCacheByImgPath(String imgPath) {
        SoftReference<Bitmap> softReference = msImgCache.get(imgPath);
        return softReference == null ? null : softReference.get();
    }

    public static synchronized Bitmap getBitmapById(Context context, int drawResid) {
        SoftReference<Bitmap> softReference = msBitmap.get(Integer.valueOf(drawResid));
        Bitmap bmp = softReference == null ? null : softReference.get();
        if ((bmp != null) && (bmp.isRecycled()) && (context != null)) {
            bmp = ((BitmapDrawable) context.getResources().getDrawable(drawResid)).getBitmap();
            msBitmap.put(Integer.valueOf(drawResid), new SoftReference<Bitmap>(bmp));
        }
        return bmp;
    }

    public static synchronized Bitmap getBitmapByIdNoCache(Context context, int drawResid) {
        synchronized (ResCacheLoadUtil.class) {
            Bitmap bmp = null;
            if (context != null) {
                bmp = BitmapFactory.decodeResource(context.getResources(), drawResid);
            }
            return bmp;
        }
    }

    public static synchronized String getStringById(Context context, int strResid) {
        String str = msString.get(Integer.valueOf(strResid));
        if (str == null) {
            str = context.getResources().getString(strResid);
            msString.put(Integer.valueOf(strResid), str);
        }
        return str;
    }

    public static synchronized Drawable getDrawableById(Context context, int drawResid) {
        SoftReference<Drawable> softReference = msDrawable.get(Integer.valueOf(drawResid));
        Drawable drawable = softReference == null ? null : softReference.get();
        if (drawable == null) {
            drawable = context.getResources().getDrawable(drawResid);
            msDrawable.put(Integer.valueOf(drawResid), new SoftReference<Drawable>(drawable));
        }
        return drawable;
    }

    public static synchronized void clean() {
        msBitmap.evictAll();
        msDrawable.evictAll();
        msString.evictAll();
    }
}
